﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Message
{
    public partial class Form : FormPage
    {
        BLL.Message bll = new BLL.Message();
        Model.Message model = new Model.Message();
        //
        string ReceiveIDStr = "";
        BLL.PAUser bllPAUser = new BLL.PAUser();
        List<Model.PAUser> listModelPAUser = new List<Model.PAUser>();
        string ReceiveUsernameStr = "";
        protected void Page_Load(object sender, EventArgs e) { DataTypePlus.getPageRequest(Request.QueryString["ReceiveIDStr"], ref ReceiveIDStr); }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id);
            ReceiveIDStr = DataTypePlus.requestString(DataTypePlus.requestInt(model.UserID));//回复收到的信息
            if (ReceiveIDStr == "0") ReceiveIDStr = "";
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            if (Security.GetCurrentUserType() == EnumHelper.UserType.Administrator)
            {
                if (ReceiveIDStr != "")
                {
                    listModelPAUser = bllPAUser.GetModelList("id in (" + ReceiveIDStr + ")");
                    foreach (Model.PAUser pauser in listModelPAUser) ReceiveUsernameStr += (ReceiveUsernameStr != "" ? "," : "") + pauser.Username;
                    lb_ReceiveUserName.Text = ReceiveUsernameStr;
                    ddl_ReceiveUserType.Visible = false;
                }
                else
                {
                    lb_ReceiveUserName.Visible = false;
                    ddl_ReceiveUserType.Items.Add(new ListItem("全部", "0"));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Store), ((int)EnumHelper.UserType.Store).ToString()));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Supplier), ((int)EnumHelper.UserType.Supplier).ToString()));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.StoreServer), ((int)EnumHelper.UserType.StoreServer).ToString()));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.SupplierServer), ((int)EnumHelper.UserType.SupplierServer).ToString()));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Accountant), ((int)EnumHelper.UserType.Accountant).ToString()));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Buyer), ((int)EnumHelper.UserType.Buyer).ToString()));
                    ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Receiver), ((int)EnumHelper.UserType.Receiver).ToString()));
                    ddl_ReceiveUserType.SelectedIndex = 0;
                }
            }
            else
            {
                lb_ReceiveUserName.Visible = false;
                ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Administrator), ((int)EnumHelper.UserType.Administrator).ToString()));
                switch (Security.GetCurrentUserType())
                {
                    case EnumHelper.UserType.Store:
                        ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.StoreServer), ((int)EnumHelper.UserType.StoreServer).ToString()));
                        break;
                    case EnumHelper.UserType.Supplier:
                        ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.SupplierServer), ((int)EnumHelper.UserType.SupplierServer).ToString()));
                        break;
                    case EnumHelper.UserType.StoreServer:
                        ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Store), ((int)EnumHelper.UserType.Store).ToString()));
                        break;
                    case EnumHelper.UserType.SupplierServer:
                        ddl_ReceiveUserType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.UserType.Supplier), ((int)EnumHelper.UserType.Supplier).ToString()));
                        break;
                }
                ddl_ReceiveUserType.SelectedIndex = 0;
            }
            //
            if (Security.InUserType(new List<EnumHelper.UserType> { EnumHelper.UserType.Supplier, EnumHelper.UserType.Store })) group_IsSendSMS.Visible = false;
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            //回复收到的信息
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            string TheContent = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_TheContent.Text));

            int userID = Security.GetCurrentUserID();
            bool IsSendSMS = cb_IsSendSMS.Checked;
            if (ReceiveIDStr != "") bll.NewMessage(Master.id, userID, ReceiveIDStr, TheContent, IsSendSMS,true);
            else
            {
                ReceiveUsernameStr = ddl_ReceiveUserType.SelectedItem.Text;
                bll.NewMessage2UserType(Master.id, userID, DataTypePlus.requestInt(ddl_ReceiveUserType.SelectedValue), TheContent, IsSendSMS,true);
            }
            BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Message, Master.id, EnumHelper.OperateType.Add, (Master.formState == EnumHelper.FormState.add ? "发送" : "回复" + Master.id) + "给" + ReceiveUsernameStr + ":" + TheContent);
            Master.SubmitOK();


        }

    }
}